<?php
namespace app\daifast\controller; 
use \think\Db; 


class Advert extends \think\Controller
{
     /**
     * 专家列表
     */
    public function index(){
          
        //获取广告位分类
        $cArr = Db('sys_ad_type')->select();
        $catagoryArr = array_column($cArr, "names","id");
        
        $data = Db('sys_ad')->order("id desc")->paginate(5); 
        $listArr = $data->items();
        $pages = $data->render(); 
        
        return $this->allFetch(get_defined_vars());
    }
     
    /**
     * 删除
     */
    public function delete(){
        $aid = intval(input('aid'));
        
        Db('sys_ad')->where(['id'=>$aid])->delete();
        
        $this->showMsg('删除成功', url('Advert/index'));
        
    }
    
    
    /**
     * 添加专家
     * @return mixed
     */
    public function add(){
      //获取广告位信息
      $aid = intval(input('aid'));
      
      $aid and $AdvertInfo = Db('sys_ad')->where(['id'=>$aid])->find();
      //获取广告位分类
      $cArr = Db('sys_ad_type')->select();
      $catagoryArr = array_column($cArr, "names","id");
   
      //编辑或添加
      if(input('sbt')){
            $aid = intval(input('aid'));
            $data['title'] = input('title');
            $data['path'] = input('path');
            $data['code'] = input('code');
            $data['url'] = input('url'); 
            $data['on_time'] = time();
            //鞋库
            $aid and Db('sys_ad')->where('id',$aid)->update($data);
            $aid or Db('sys_ad')->insert($data); 
            $this->showMsg('操作成功', url('Advert/index'));
        }
        
        return $this->allFetch(get_defined_vars());
    }
    
    
    /**
     * 广告位分类管理
     * @return mixed
     */
    public function category(){
       
        //获取广告位分类
        $listArr = Db('sys_ad_type')->select(); 
         
        return $this->allFetch(get_defined_vars());
    }
    
    /**
     * 广告位分类添加
     * @return mixed
     */
    public function categoryAdd(){
        $data['names'] = input('names');
        $data['on_time'] = time();
        $lastId = Db('sys_ad_type')->insertGetId($data);
        
        $html = '
                <tr id="tr_'.$lastId.'">
                <td>'.$data['names'].'</td>
                <td>
                <a href="javascript:void(0);" data="'.url('Advert/categoryEdit',['cid'=>$lastId]).'" class="tablelink" onclick="editCategory(this)">编辑</a> 
                <a href="'.url('Advert/categoryDelete',['cid'=>$lastId]).'" class="tablelink" >删除</a></td>
                </tr>'
        ;
        
        echojson('ok', 1,$html);
        
        
        
    }
    
    
    /**
     * 广告位分类删除
     * @return mixed
     */
    public function categoryDelete(){
        
        $cid = intval(input('cid'));
        
        Db('sys_ad_type')->where(['id'=>$cid])->delete();
    
        $this->showMsg('操作成功', url('Advert/category'));
    }
    
    
    
    /**
     * 广告位分类添加
     * @return mixed
     */
    
    public function categoryEdit(){
        
        
        $cid = intval(input('cid'));
        
        if(input('ajax')){
            $category = input('category');
            Db('sys_ad_type')->where(['id'=>$cid])->update(['names'=>$category]);
            echojson('ok', 1);
        }
        
        $categoryInfo = Db('sys_ad_type')->where(['id'=>$cid])->find();
        
        return $this->allFetch(get_defined_vars());
    }
    
   
}
